﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonGameClasses;
using MarvinsArena.Robot.Core;
using MarvinsArena.Core;

namespace BattleLogic
{
	public class Robot:IRobot
	{
		private RobotLoader loader;
		private RobotHost robotHost;

		public string Name { get; set; }
		public double PositionX { get { return this.robotHost.PositionX; } set { this.robotHost.PositionX = value; } }
		public double PositionY { get { return this.robotHost.PositionY; } set { this.robotHost.PositionY = value; } }
		public double Radius { get { return 12.0f; } }
		public double Rotation { get { return this.robotHost.Rotation; } set { this.robotHost.Rotation = value; } }
		public TypeWorldObjects TypeWorldObject { get { return TypeWorldObjects.Robot; } }


		public double RotationGun { get { return this.robotHost.RotationGun; } set { this.robotHost.RotationGun = value; } }
		public double RotationRadar { get { return this.robotHost.RotationRadar; } set { this.robotHost.RotationRadar = value; } }
		public int SquadNumber { get { return this.robotHost.SquadNumber; } }
		public int Team { get { return this.robotHost.Team; } }
		public string FullName { get { return String.Format("{0} {1}.{2}", this.robotHost.Name, this.robotHost.Team, this.robotHost.SquadNumber); } }
		public int Hitpoints { get { return this.robotHost.Hitpoints; } }
		public int Missiles { get { return this.robotHost.Missiles; } }
		public int Heat { get { return this.robotHost.Heat; } }
		public bool IsActive { get { return this.robotHost.IsActive; } }
		public string DisabledMessage { get { return this.robotHost.DisabledMessage; } }
		public event EventHandler<MessageExchangeFromRobotEventArgs> HandleRobotEvents { add { } remove { } }


		public Robot(RobotLoader loader)
		{
			this.loader = loader;
			this.robotHost = loader.RobotHost;
		}

		public bool ObjectInScannerRange(IWorldObject target)
		{
			double scanRadius = 0.69813170079773183076947630739545; //40°
			double scanRadiusHalf = scanRadius / 2.0;

			double scannerAngleInRad = this.RotationRadar - scanRadiusHalf;
			double scannerAngleInRad2 = this.RotationRadar + scanRadiusHalf;
			double angleBetweenObjects = Math.Atan2(target.PositionY - this.PositionY, target.PositionX - this.PositionX);

			if (angleBetweenObjects >= scannerAngleInRad && angleBetweenObjects <= scannerAngleInRad2)
				return true;

			if (scannerAngleInRad < -Math.PI && angleBetweenObjects == Math.PI && target.PositionX < this.PositionX)
				return true;

			return false;
		}

		//public override void Update(double deltaTime)
		//{
		//    System.Threading.Thread threadRun = new System.Threading.Thread(this.robotHost.Run);
		//    threadRun.Start();

		//    if (CoreMain.Instance.Timeout)
		//    {
		//        // Terminate after 250ms
		//        if (!threadRun.Join(250))
		//        {
		//            this.robotHost.Disable("Timeout of 250ms reached");
		//        }
		//    }
		//    else
		//    {
		//        threadRun.Join();
		//    }

		//    MessageExchangeFromRobotEventArgs ex = this.robotHost.Update(deltaTime);

		//    if (HandleRobotEvents != null)
		//    {
		//        HandleRobotEvents(this, ex);
		//    }
		//}

		public void NotifyMessageToTeammate(MarvinsArena.Robot.Core.CoreMessageReceivedFromTeammateEventArgs msg)
		{
			this.robotHost.NotifyMessageToTeammate(msg);
		}

		public void NotifyCollision(Collision collision)
		{
			this.robotHost.NotifyCollision(collision);
		}

		public void NotifyTarget(ScannerTarget scannerTarget)
		{
			this.robotHost.NotifyTarget(scannerTarget);
		}

		public void Notify()
		{
			this.robotHost.Notify();
		}

		public void SetRoundValues(TournamentRules rules)
		{
			this.robotHost.SetRoundValues(rules.Hitpoints, rules.Missiles, rules.MaxHeat);
		}
	}
}
